<template>
  <div class="register-container">
    <div class="logo-container">
      <img src="@/assets/logo.webp" alt="Logo" class="logo">
    </div>
    <div class="register-header">
      <div class="header-right">
        <h1>欢迎注册我们的招聘平台</h1>
        <p>加入我们，开启您的职业生涯之旅。</p>
      </div>
    </div>
    <div class="content-wrapper">
      <div class="register-section">
        <div class="register-form">
          <form @submit.prevent="handleRegister">
            <div class="form-group">
              <label for="username">用户名:</label>
              <input placeholder="请输入用户名" type="text" id="username" v-model="username" required />
            </div>
            <div class="form-group">
              <label for="password">密码:</label>
              <input placeholder="请输入密码" type="password" id="password" v-model="password" required />
            </div>
            <div class="form-group">
              <label for="confirmPassword">确认密码:</label>
              <input placeholder="请再次输入密码" type="password" id="confirmPassword" v-model="confirmPassword" required />
            </div>
            <div class="form-group">
              <label for="email">邮箱:</label>
              <input placeholder="请输入邮箱" type="email" id="email" v-model="email" required />
            </div>
            <div class="form-group">
              <label for="phone">手机号:</label>
              <input placeholder="请输入手机号" type="tel" id="phone" v-model="phone" required />
            </div>
            <div class="form-group" v-if="role === 'employee'">
              <label for="company">公司:</label>
              <select id="company" v-model="selectedCompany" required>
                <option value="" disabled selected>请选择公司</option>
                <option v-for="company in companies" :key="company.id" :value="company.id">
                  {{ company.name }}
                </option>
              </select>
            </div>
            <button type="submit">注册</button>
          </form>
        </div>
      </div>
    </div>
    <Footer />
  </div>
  <el-dialog
      title="请选择您的角色"
      v-model="dialogVisible"
      width="30%"
      :before-close="handleClose"
      custom-class="custom-dialog"
  >
    <span style="height:50px;width: auto">
      <el-button type="primary" @click="selectRole('job-seeker')">我是求职者</el-button>
      <el-button type="primary" @click="selectRole('employee')">我是招聘者</el-button>
    </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">返 回</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import Footer from '@/components/Footer.vue';

import 'element-plus/dist/index.css';
import axios from '@/utils/axios'; // 使用配置好的axios实例

const role = ref('');
const dialogVisible = ref(true);
const router = useRouter();
const user  = ref(getUser());
const username = ref('');
const password = ref('');
const email = ref('');
const phone = ref('');
const confirmPassword = ref('');
const selectedCompany = ref('');
const companies = ref([]);

const selectRole = (selectedRole) => {
  role.value = selectedRole;
  dialogVisible.value = false;
};

const fetchCompanies = () => {
  axios.get(BASEURL+'/employee/selectCompanies').then((response) => {
    if (response.data.code === 2000) {
      console.log('公司列表:', response.data);
      companies.value = response.data.data.map((name, index) => ({
        id: index + 1, // 假设 id 从 1 开始
        name: name
      }))
    } else {
      console.error('获取公司列表失败:', response.data.message);
    }
  }).catch(error => {
    console.error('获取公司列表失败:', error);
  });
};

onMounted(() => {
  fetchCompanies();
});

const handleRegister = () => {
  if (password.value !== confirmPassword.value) {
    alert('两次输入的密码不一致');
    return;
  }

  // 构建请求数据
  let roleCode;
  if (role.value === 'job-seeker') {
    roleCode = 1; // 假设 job-seeker 对应 1
  } else if (role.value === 'employee') {
    roleCode = 2; // 假设 employee 对应 2
  } else {
    alert('请选择您的角色');
    return;
  }

  const requestData = {
    username: username.value,
    password: password.value,
    email: email.value,
    phone: phone.value,
    role: roleCode,
    companyId: role.value === 'employee' ? selectedCompany.value : null
  };

  // 发送注册请求
  axios.post(BASEURL+'/common/reg', requestData)
      .then(response => {
        if (response.data.code === 2000) {
          // 注册成功
          const token = response.data.token;
          localStorage.setItem('token', token);
          localStorage.setItem('role', role.value);
          if (role.value === 'employee') {
            localStorage.setItem('companyId', selectedCompany.value); // 存储选中的公司ID
          }
          router.push('/login');
        } else {
          // 处理注册失败的情况
          alert('注册失败: ' + response.data.message);
        }
      })
      .catch(error => {
        console.error('注册请求失败:', error);
        alert('注册请求失败，请稍后再试');
      });
};

const handleClose = () => {
  dialogVisible.value = false;
  router.push('/login'); // 点击取消返回登录页面
};
</script>

<style scoped>
.register-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: linear-gradient(135deg, #2535f5 30%, #6af1ff 70%);
  padding: 20px;
  font-family: 'Arial', sans-serif;
}

.logo-container {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 1000;
}

.logo-container .logo {
  width: 100px; /* 根据实际情况调整大小 */
  height: auto;
}

.register-header {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  color: white;

  margin-bottom: 100px; /* 调整顶部边距以避开 logo */
}

.header-right {
  margin-left: 0; /* 移除左边距 */
}

.register-header h1 {
  font-size: 28px;
  margin: 0;
}

.register-header p {
  font-size: 18px;
  margin: 10px 0 0;
}

.content-wrapper {
  display: flex;
  justify-content: center;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.register-section {
  flex: 1;
}

.register-form {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
  transition: box-shadow 0.3s ease;
}

.register-form:hover {
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 16px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.form-group input:focus {
  border-color: #007bff;
  box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}

button[type="submit"] {
  width: 100%;
  padding: 14px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s ease, transform 0.2s ease;
}

button[type="submit"]:hover {
  background-color: #0056b3;
  transform: scale(1.05);
}

@media (max-width: 768px) {
  .content-wrapper {
    flex-direction: column;
  }

  .register-section {
    width: 100%;
    margin: 0 0 20px;
  }
}
</style>
